package com.dingding.queue.base.push;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.config.JPushConfig;
import com.dingding.facade.queue.enums.RoleType;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.IosNotification.Builder;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 
 * @ClassName: IosSendPush 
 * @Description: IOS 消息推送实现类
 * @author CHEHAUNBO 
 * @date 2016年6月13日 上午9:58:18
 * 
 */
@Component("iosSendPush")
@SuppressWarnings("all")
public class IosSendPush extends AbstractSendPush {

	private static final Logger log = Logger.getLogger(IosSendPush.class);

	@Override
	public boolean sendPush(String aliasId, String alert, String msgTitle, String msgContent, Map<String, String> map,
			RoleType roleType) {

		Builder builder = IosNotification.newBuilder();
		for (String key : map.keySet()) {
			builder.addExtra(key, map.get(key));
		}
		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.ios()) // 推送平台iOS
				// 设置iOS设备别名
				.setAudience(Audience.alias(aliasId))
				// 设置iOS通知及声音
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(builder.setAlert(alert).setSound("sounde.caf").build()).build())
				// 设置消息标题及内容
				.setMessage(Message.newBuilder().setMsgContent(msgContent).build())
				// 设置推送环境 true 生产环境 false 测试环境
				.setOptions(Options.newBuilder().setApnsProduction(JPushConfig.isENV_JPUSH()).build()).build();

		return excuteSendPush(pushPayload, roleType);

	}

	/**
	 * 
	 * 推送 只包含（通知、自定义参数）
	 * 
	 * @Title: sendPush 
	 * @param aliasId 推送别名
	 * @param alert  通知
	 * @param map 自定义参数
	 * @return  boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public boolean sendPush(String aliasId, String alert, Map<String, String> map, RoleType roleType) {

		Builder builder = IosNotification.newBuilder();
		for (String key : map.keySet()) {
			builder.addExtra(key, map.get(key));
		}
		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.ios()) // 推送平台iOS
				// 设置iOS设备别名
				.setAudience(Audience.alias(aliasId))
				// 设置iOS通知及声音
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(builder.setAlert(alert).setSound("sounde.caf").build()).build())
				// 设置推送环境 true 生产环境 false 测试环境
				.setOptions(Options.newBuilder().setApnsProduction(JPushConfig.isENV_JPUSH()).build()).build();

		return excuteSendPush(pushPayload, roleType);

	}

	/**
	 * 
	 * 推送消息，不包含(自定义标题)
	 * 
	 * @Description:封装iOS消息推送参数 
	 * @param aliasId 设备别名 
	 * @param alert 通知 
	 * @param msgContent 自定义消息内容 
	 * @author cheshaowei
	 *  
	 */
	@Override
	public boolean sendPush(String aliasId, String alert, String msgContent, RoleType roleType) {

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.ios()) // 推送平台iOS
				// 设置iOS设备别名
				.setAudience(Audience.alias(aliasId))
				// 设置iOS通知及声音
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(
								IosNotification.newBuilder().setAlert(alert).setSound("sounde.caf").build())
						.build())
				// 设置消息标题及内容
				.setMessage(Message.newBuilder().setMsgContent(msgContent).build())
				// 设置推送环境 true 生产环境 false 测试环境
				.setOptions(Options.newBuilder().setApnsProduction(JPushConfig.isENV_JPUSH()).build()).build();

		return excuteSendPush(pushPayload, roleType);

	}

	@Override
	public boolean sendPush(String aliasId, String msgContent, RoleType roleType) {

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.ios()) // 推送平台iOS
				// 设置iOS设备别名
				.setAudience(Audience.alias(aliasId))
				// 设置消息标题及内容
				.setMessage(Message.newBuilder().setMsgContent(msgContent).build())
				// 设置推送环境 true 生产环境 false 测试环境
				.setOptions(Options.newBuilder().setApnsProduction(JPushConfig.isENV_JPUSH()).build()).build();

		return excuteSendPush(pushPayload, roleType);
	}

	/**
	 * 执行消息推送
	 */
	@Override
	public boolean excuteSendPush(PushPayload pushPayload, RoleType roleType) {

		try {

			PushResult pushResult = null;

			if (RoleType.Driver == roleType) {
				pushResult = super.driverJpushClient.sendPush(pushPayload);
			} else {
				pushResult = super.iosJPushClient.sendPush(pushPayload);
			}

			if (pushResult != null && pushResult.isResultOK()) {
				log.info("推送结果：" + pushResult.isResultOK());
				return true;
			}

		} catch (APIConnectionException e) {
			log.error("极光发送消息出现异常！", e);
		} catch (APIRequestException e) {
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message Id: " + e.getMsgId());
			log.info("Error Message: " + e.getErrorMessage());
		}

		return false;

	}

}
